home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / vfs / names.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  3.1 KB  |  134 lines

  1. /* Look up user and/or group names.
  2.    Copyright (C) 1988, 1992 Free Software Foundation
  3.  
  4.    From GNU Tar.
  5.    
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Look up user and/or group names.
  22.  *
  23.  * This file should be modified for non-unix systems to do something
  24.  * reasonable.
  25.  */
  26.  
  27. #include <config.h>
  28. #include <sys/types.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #define TAR_NAMES
  32. #include "tar.h"
  33. #include "names.h"
  34.  
  35. #include <stdio.h>
  36. #include <pwd.h>
  37. #include <grp.h>
  38.  
  39. static int saveuid = -993;
  40. static char saveuname[TUNMLEN];
  41. static int my_uid = -993;
  42.  
  43. static int savegid = -993;
  44. static char savegname[TGNMLEN];
  45. static int my_gid = -993;
  46.  
  47. #define myuid    ( my_uid < 0? (my_uid = getuid()): my_uid )
  48. #define    mygid    ( my_gid < 0? (my_gid = getgid()): my_gid )
  49.  
  50. /*
  51.  * Look up a user or group name from a uid/gid, maintaining a cache.
  52.  * FIXME, for now it's a one-entry cache.
  53.  * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  54.  *
  55.  * This is ifdef'd because on Suns, it drags in about 38K of "yellow
  56.  * pages" code, roughly doubling the program size.  Thanks guys.
  57.  */
  58. void finduname (char *uname, int uid)
  59. {
  60.     struct passwd *pw;
  61. #ifndef HAVE_GETPWUID
  62.     extern struct passwd *getpwuid ();
  63. #endif
  64.  
  65.     if (uid != saveuid) {
  66.     saveuid = uid;
  67.     saveuname[0] = '\0';
  68.     pw = getpwuid (uid);
  69.     if (pw)
  70.         strncpy (saveuname, pw->pw_name, TUNMLEN);
  71.     }
  72.     strncpy (uname, saveuname, TUNMLEN);
  73. }
  74.  
  75. int finduid (char *uname)
  76. {
  77.     struct passwd *pw;
  78.     extern struct passwd *getpwnam ();
  79.     
  80.     saveuid = 0;
  81.  
  82.     if (uname[0] != saveuname[0]/* Quick test w/o proc call */
  83.     ||0 != strncmp (uname, saveuname, TUNMLEN)) {
  84.     strncpy (saveuname, uname, TUNMLEN);
  85.     pw = getpwnam (uname);
  86.     if (pw) {
  87.         saveuid = pw->pw_uid;
  88.     } else {
  89.         saveuid = myuid;
  90.     }
  91.     }
  92.     return saveuid;
  93. }
  94.  
  95.  
  96. void findgname (char *gname, int gid)
  97. {
  98.     struct group *gr;
  99. #ifndef HAVE_GETGRGID
  100.     extern struct group *getgrgid ();
  101. #endif
  102.  
  103.     if (gid != savegid) {
  104.     savegid = gid;
  105.     savegname[0] = '\0';
  106.     (void) setgrent ();
  107.     gr = getgrgid (gid);
  108.     if (gr)
  109.         strncpy (savegname, gr->gr_name, TGNMLEN);
  110.     }
  111.     (void) strncpy (gname, savegname, TGNMLEN);
  112. }
  113.  
  114.  
  115. int findgid (char *gname)
  116. {
  117.     struct group *gr;
  118.     extern struct group *getgrnam ();
  119.     
  120.     savegid = 0;
  121.  
  122.     if (gname[0] != savegname[0]/* Quick test w/o proc call */
  123.     ||0 != strncmp (gname, savegname, TUNMLEN)) {
  124.     strncpy (savegname, gname, TUNMLEN);
  125.     gr = getgrnam (gname);
  126.     if (gr) {
  127.         savegid = gr->gr_gid;
  128.     } else {
  129.         savegid = mygid;
  130.     }
  131.     }
  132.     return savegid;
  133. }
  134.